/*   
 * Copyright (c) 2016-2020 canaanQd. All Rights Reserved.   
 *   
 * This software is the confidential and proprietary information of   
 * canaanQd. You shall not disclose such Confidential Information   
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with canaanQd.   
 *   
 */ 

package com.lg.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lg.utils.StringUtils;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
	 private Logger logger = LoggerFactory.getLogger(this.getClass());

	  @Value("${spring.redis.host}")
	  private String host;

	  @Value("${spring.redis.port}")
	  private int port;

	  @Value("${spring.redis.timeout}")
	  private int timeout;

	  @Value("${spring.redis.password}")
	  private String password;

	  @Value("${spring.redis.database}")
	  private int database;

	  @Value("${spring.redis.pool.max-idle}")
	  private int maxIdle;

	  @Value("${spring.redis.pool.min-idle}") 
	  private int minIdle;

	  /**
	   * redis模板，存储关键字是字符串，值是Jdk序列化
	   * @Description:
	   * @param factory
	   * @return
	   */
	  @Bean
	  public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {
	      RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
	      //redisTemplate.setConnectionFactory(factory);
	      redisTemplate.setConnectionFactory(getConnectionFactory());
	      RedisSerializer<String> redisSerializer = new StringRedisSerializer();
	      redisTemplate.setKeySerializer(redisSerializer);
	      redisTemplate.setHashKeySerializer(redisSerializer);
	      //JdkSerializationRedisSerializer序列化方式;
	      JdkSerializationRedisSerializer jdkRedisSerializer=new JdkSerializationRedisSerializer();
	      //2选一
	      //Jackson 序列化方式
	      Jackson2JsonRedisSerializer<Object> jacksonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
	      ObjectMapper objectMapper = new ObjectMapper();
	      objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	      objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	      jacksonRedisSerializer.setObjectMapper(objectMapper);
	      redisTemplate.setValueSerializer(jacksonRedisSerializer);
	      redisTemplate.setHashValueSerializer(jacksonRedisSerializer);
	      redisTemplate.afterPropertiesSet();
	      return redisTemplate; 
	  }
	  
	    @Bean
	    public JedisPool jedisPool() {
	        logger.info("JedisPool注入成功！！");
	        logger.info("redis地址：" + host + ":" + port);
	        JedisPool jedisPool = new JedisPool(jedisPoolConfig(), host, port, timeout,null,1);
	        if(StringUtils.isNotEmpty(password)){
	        	jedisPool = new JedisPool(jedisPoolConfig(), host, port, timeout,password,1);
	        }
	        return jedisPool;
	    }
	    
	    @Bean
	    JedisPoolConfig jedisPoolConfig(){
    	    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
	        jedisPoolConfig.setMaxIdle(maxIdle);
	        jedisPoolConfig.setMaxWaitMillis(2);
	        return jedisPoolConfig;
	    }
	    
	    @SuppressWarnings("deprecation")
		@Bean  
	    public JedisConnectionFactory getConnectionFactory(){  
	        JedisConnectionFactory factory = new JedisConnectionFactory();  
	        factory.setHostName(host);
	        factory.setPort(port);
	        factory.setTimeout(timeout);
	        factory.setPassword(password);
	        factory.setDatabase(1);
	        factory.setPoolConfig(jedisPoolConfig());
	        return factory;
	    }
	    
}
